<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产品主图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chanpzt">
              <j-image-upload isMultiple v-model="model.chanpzt"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chanpmc">
              <a-input v-model="model.chanpmc" placeholder="请输入产品名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="能源类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nengylx">
              <j-dict-select-tag type="list" v-model="model.nengylx" dictCode="nengylx" placeholder="请选择能源类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="适用品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shiypp">
              <a-select mode="multiple" placeholder="请选择品牌" @change="handleChange" show-search>
                <a-select-option v-for="(item, index) in pinpList" :key="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产品描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chanpmc">
              <a-input v-model="model.chanpms" placeholder="请输入产品描述"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产品详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chanpxq">
              <j-editor v-model="model.chanpxq" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="保障范围" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="baozfw">
              <j-editor v-model="model.baozfw" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="服务条款" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fuwtk">
              <j-editor v-model="model.fuwtk" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'KuybChanpForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      pinpList: [],
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        nengylx: [{ required: true, message: '请选择能源类型!' }],
        chanpmc: [{ required: true, message: '请输入产品名称!' }],
        toufdq: [{ required: true, message: '请输入投放地区!' }],
      },
      url: {
        add: '/chanp/kuybChanp/add',
        edit: '/chanp/kuybChanp/edit',
        queryById: '/chanp/kuybChanp/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    getAction('/chexkPinp/kuybChexkPinp/list', { pageNo: 1, pageSize: 999 })
      .then((res) => {
        if (res.success) {
          this.pinpList = res.result.records
        } else {
          this.$error({ title: '提示', content: res.message })
        }
      })
      .finally(() => {})
  },
  methods: {
    handleChange(value) {
      console.log(`selected ${value}`)
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>